package com.tzy.for10evalue.haoyong.excelexport.consumerservice;

import com.aebiz.b2bdeliproduct.factory.ReportImportHandler;
import com.aebiz.deli.service.ExcelDynamicService;
import com.google.common.collect.Maps;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;

/**
 * @author:yzw
 * @create: 2022-05-11 16:10:00
 * @Description: 服务-类型-工厂
 */
@Component
public class ServiceFactory implements InitializingBean, ApplicationContextAware {

    private static final Map<Integer, ReportHandler> serviceMap = Maps.newHashMap();

    private static final Map<Integer, ReportImportHandler> importServiceMap = Maps.newHashMap();

    private static final Map<Integer, CheckParamHandler> checkServiceMap = Maps.newHashMap();

    private static final Map<Integer, ExcelDynamicService> excelDynamicServiceMap = Maps.newHashMap();

    private static final Map<Integer, ProductRiskHandler> productRiskMap = Maps.newHashMap();

    private static final Map<Integer, CategoryConfigHandler> categoryConfigHandlerMap = Maps.newHashMap();

    private static final Map<Integer, ProductBatchImportNewHandler> productBatchImportNewHandlerMap = Maps.newHashMap();

    private ApplicationContext appContext;


    public static ExcelDynamicService getExcelDynamicService(Integer reportType) {
        if (reportType == null) {
            throw new IllegalArgumentException("service model is empty.");
        }
        if (!excelDynamicServiceMap.containsKey(reportType)) {
            throw new IllegalArgumentException("service model not supported.");
        }
        return excelDynamicServiceMap.get(reportType);
    }

    public static ReportHandler getReportHandler(Integer model) {
        if (model == null) {
            throw new IllegalArgumentException("handler model is empty.");
        }
        if (!serviceMap.containsKey(model)) {
            throw new IllegalArgumentException("handler model not supported.");
        }
        return serviceMap.get(model);
    }


    public static ReportImportHandler getImportReportHandler(Integer type) {
        if (Objects.isNull(type)) {
            throw new IllegalArgumentException("handler model is empty.");
        }
        if (!importServiceMap.containsKey(type)) {
            throw new IllegalArgumentException("handler model not supported.");
        }
        return importServiceMap.get(type);
    }

    public static CheckParamHandler getReportCheckHandler(Integer type) {
        if (Objects.isNull(type)) {
            throw new IllegalArgumentException("handler model is empty.");
        }
        if (!checkServiceMap.containsKey(type)) {
            throw new IllegalArgumentException("handler model not supported.");
        }
        return checkServiceMap.get(type);
    }

    public static ProductRiskHandler getProductRiskHandler(Integer type) {
        if (Objects.isNull(type)) {
            throw new IllegalArgumentException("handler model is empty.");
        }
        if (!productRiskMap.containsKey(type)) {
            throw new IllegalArgumentException("handler model not supported.");
        }
        return productRiskMap.get(type);
    }

    public static CategoryConfigHandler getCategoryConfigHandler(Integer type) {
        if (Objects.isNull(type)) {
            throw new IllegalArgumentException("handler model is empty.");
        }
        if (!categoryConfigHandlerMap.containsKey(type)) {
            throw new IllegalArgumentException("handler model not supported.");
        }
        return categoryConfigHandlerMap.get(type);
    }

    public static ProductBatchImportNewHandler getProductBatchImportNewHandler(Integer type) {
        if (Objects.isNull(type)) {
            throw new IllegalArgumentException("batchImportHandler model is empty.");
        }
        if (!productBatchImportNewHandlerMap.containsKey(type)) {
            throw new IllegalArgumentException("batchImportHandler model not supported.");
        }
        return productBatchImportNewHandlerMap.get(type);
    }



    @Override
    public void afterPropertiesSet() throws Exception {
        // 将 Spring 容器中所有的 CalculateModel 接口实现类注册到 strategies
        appContext.getBeansOfType(ReportHandler.class)
                .values()
                .forEach(handler -> serviceMap.put(handler.getReportType(), handler));
        appContext.getBeansOfType(ReportImportHandler.class)
                .values()
                .forEach(handler -> importServiceMap.put(handler.getType(), handler));
        appContext.getBeansOfType(CheckParamHandler.class)
                .values()
                .forEach(handler -> checkServiceMap.put(handler.getOpeType(), handler));
        // 将 Spring 容器中所有的 CalculateModel 接口实现类注册到 strategies
        appContext.getBeansOfType(ExcelDynamicService.class)
                .values()
                .forEach(handler -> excelDynamicServiceMap.put(handler.getType(), handler));
        appContext.getBeansOfType(CategoryConfigHandler.class)
                .values()
                .forEach(handler -> categoryConfigHandlerMap.put(handler.getType(), handler));
        appContext.getBeansOfType(ProductRiskHandler.class)
                .values()
                .forEach(handler -> productRiskMap.put(handler.getType(), handler));
        appContext.getBeansOfType(ProductRiskHandler.class)
                .values()
                .forEach(handler -> productRiskMap.put(handler.getType(), handler));
        appContext.getBeansOfType(ProductBatchImportNewHandler.class)
                .values()
                .forEach(handler -> productBatchImportNewHandlerMap.put(handler.getType(), handler));
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        appContext = applicationContext;
    }
}
